﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Dragon.Prototype1.Infrastructure.Controls
{
    public class CheckBox : jQueryControl
    {
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Input;
            }
        }

        public CheckBox(string name)
            : this(name, name, string.Empty, string.Empty, false)
        { 
        
        }

        public CheckBox(string name,bool isChecked)
            : this(name, name, string.Empty, string.Empty, isChecked)
        {

        }

        public CheckBox(string name, string text, bool isChecked)
            :this(name,name,text,"",isChecked)
        { 
            
        }

        public CheckBox(string id,string name,string text,string value,bool isChecked)
        {
            this.ID = id;
            this.Name = name;
            this.Text = text;
            this.Value = value;
            this.Selector = string.Format("input[name={0}]",name);
            this.IsChecked = isChecked;
            CheckBoxStyleName = "jquery-ryanfait-checkbox-red";
        }

        //public bool IsRequired
        //{
        //    get;
        //    set;
        //}

        public bool IsChecked
        {
            get;
            set;
        }

        public string Text
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }

        public override void Render(HtmlTextWriter writer)
        {
            if(!string.IsNullOrEmpty(Text))
            {
                //reference: http://javascript.about.com/library/blradio1.htm
                //add "name" attribute instead of "for"
                writer.AddAttribute(HtmlTextWriterAttribute.For,this.ID);
                //writer.AddAttribute(HtmlTextWriterAttribute.Name, this.Name);
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
            }
            this.RenderBeginTag(writer);
            this.RenderContents(writer);
            this.RenderEndTag(writer);

            if(!string.IsNullOrEmpty(Text))
            {
                writer.Write(this.Text);
                writer.RenderEndTag();
            }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            if(IsChecked)
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "true");
            if (!string.IsNullOrEmpty(this.Value))
                writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Value);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "checkbox");
            base.AddAttributesToRender(writer);
        }
        
        [jQueryParameter("cls")]
        public string CheckBoxStyleName
        {
            get;
            set;
        }

        public override void RenderJavaScriptContents(HtmlTextWriter writer)
        {
            base.RenderJavaScriptContents(writer);
            if (System.Configuration.ConfigurationManager.AppSettings["AllowjQueryCheckbox"].ToLower()=="true")
                writer.WriteLine(string.Format("$(\"{0}\").checkbox({1});", this.Selector, GeneratejQueryParameterArray()));
        }
    }
}
